<?php

namespace App\Http\Controllers;

use App\Events\BottleBoxEvent;
use App\Events\BoxHolderEvent;
use App\Http\Instance\Singleton;
use App\Http\Service\Impl\HomeImpl;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
use EasyWeChat\Kernel\Messages\TextCard;
use Illuminate\Support\Facades\Log;
use EasyWeChat\Factory;

use function App\Helpers\p;

/**
 * Class IndexController
 * @package App\Http\Controllers
 */
class HomeController extends Controller
{

    private $Homeservice;
    private $Ordersservice;

    public function __construct()
    {
        $this->Homeservice   = new HomeImpl();
    }


    /**
     * @param Request $request
     * @return string
     * 192.168.13.2:8078/newstar/public/bpmFiles
     */
    public function bpmFiles(Request $request)
    {
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        $djh       = $request->djh;
        $wjm       = $request->wjm;
        $url       = "http://192.168.6.123:31395/api/files/";
        $wjmarr    = explode(",", $wjm);
        $newwjmarr = [];
        foreach ($wjmarr as $k => $v) {
            $newurl      = $url . $v;
            $newwjmarr[] = $newurl;
        }
        return view("Home.bpm.bpmFiles", ["djh" => $djh, "newwjmarr" => $newwjmarr,]);
    }

    /**
     * 宿舍报修页面
     * http://wx.newamstar.com:8078/newstar/public/ssbxPages
     */
    public function ssbxPages()
    {
        return view("Home.weixin.ssbxtask");
    }


    /**
     * BPM售后上传图片-企业微信-移动端
     * http://wx.newamstar.com:8078/newstar/public/bpmsh1
     * http://localhost/sh/public/bpmsh1
     */
    public function bpmsh1()
    {
        return view("Home.weixin.bpmsh1");
    }

    public function uploadBpmPics(Request $request)
    {
        $res["msg"] = "app/bpmshuploads" . '/' . date('Ymd', time()) . '/' . bxuploadfile2($request, "ssbxuploads");
        return json_encode($res);
    }


    //保存售后提交的数据
    public function saveBpmdata(Request $request)
    {
        $data = array(
            "wlbm"        => $request->wlbm,
            "wlmc"        => $request->wlmc,
            "cz"          => $request->cz,
            "tj"          => $request->tj,
            "jz"          => $request->jz,
            "yt"          => $request->yt,
            "valid"       => '有效',
            "gnms"        => $request->gnms,
            "imgs"        => $request->imgs,
        );
        if ($request->type == 1) {
            //新增
            $data['create_time'] = date("Y-m-d H:i:s", time());
            $s = DB::connection('sh')->table("wllist")->insert($data);
            if ($s) {
                $res = ["msg" => "新增成功", "data" => [], "flag" => 1];
            } else {
                $res = ["msg" => "新增失败", "data" => [], "flag" => 0];
            }
        } else if ($request->type == 2) {
            //更新
            $data['update_time'] = date("Y-m-d H:i:s", time());
            $where['id'] = $request->id;
            $s = DB::connection('sh')->table("wllist")->where($where)->update($data);
            if ($s) {
                $res = ["msg" => "更新成功", "data" => [], "flag" => 1];
            } else {
                $res = ["msg" => "更新失败", "data" => [], "flag" => 0];
            }
        }

        return $res;
    }

    public function shdelete(Request $request)
    {
        //wlog2($request->data);
        $list = $request->data;
        $ids  = [];
        foreach ($list as $v) {
            $ids[] = $v['id'];
        }
        $dataToUpdate = [
            'valid' => '无效',
        ];
        $info = DB::connection('sh')->table("wllist")->whereIn("id", $ids)->update($dataToUpdate);
        return ['msg' => '作废成功'];
    }

    public function shundo(Request $request)
    {
        $list = $request->data;
        $ids  = [];
        foreach ($list as $v) {
            $ids[] = $v['id'];
        }
        $dataToUpdate = [
            'valid' => '有效',
        ];
        $info = DB::connection('sh')->table("wllist")->whereIn("id", $ids)->update($dataToUpdate);
        return ['msg' => '恢复成功'];
    }

    //http://localhost/sh/public/goodslist
    public function goodslist(Request $request)
    {
        $wlbm        = $request->wlbm;
        $wlmc        = $request->wlmc;
        $cz          = $request->cz;
        $valid       = $request->valid;
        $where       = [];
        if ($wlbm) {
            $where[] = array("wlbm", 'like', '%' . $wlbm . '%');
        }
        if ($wlmc) {
            $where[] = array("wlmc", 'like', '%' . $wlmc . '%');
        }
        if ($cz) {
            $where[] = array("cz", 'like', '%' . $cz . '%');
        }
        if ($valid) {
            $where[] = array("valid", 'like', '%' . $valid . '%');
        }

        $perPage     = $request->rows;
        $currentPage = $request->page;
        $offset      = ($currentPage - 1) * $perPage;
        $total       = DB::connection('sh')->table("wllist")->where($where)->count();
        $list        = DB::connection('sh')->table("wllist")->where($where)->limit($perPage)->offset($offset)->orderBy("create_time", "desc")->get();

        return ["total" => $total, "code" => 0, "rows" => $list];
    }


    //http://localhost/sh/public/login pc/售后物料图片上传
    //http://wx.newamstar.com:8078/newstar/public/bpmsh1 mobile/企微
    //http://localhost/sh/public/bpmsh1
    //新增物料
    public function view_addwl1()
    {
        return view("Home.weixin.addwl");
    }
    //编辑物料
    public function view_addwl2(Request $request)
    {
        $where['id']   = $request->id; //物料编码
        $info          = DB::connection('sh')->table("wllist")->where($where)->first();
        $info->imgs    = explode(',', $info->imgs);
        return view("Home.weixin.editwl", ['info' => $info]);
    }
    //根据物料编码获取数据
    public function autogetdata(Request $request)
    {
        //hcsrm
        $wlbm  = $request->wlbm;
        $info  = DB::connection('hcsrm')->table("Inventory")->where("cInvCode", $wlbm)->first();
        return ["data" => $info, "msg" => "success", "flag" => 1];
    }

    public function loginout()
    {
        //Auth::logout();
        return Singleton::getInstance()->loginout('login');
    }

    private function p($arr)
    {
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
    }
}
